package Day2;

import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int size = in.nextInt();
            in.nextLine();
            String[] targetStrs = in.nextLine().split(" ");
            String str1 = targetStrs[0];
            String str2 = targetStrs[1];
            String[] strs = new String[size];
            for(int i = 0; i < size; i++) {
                strs[i] = in.nextLine();
            }
            int[] indexs1 = new int[size];
            int index1 = 0;
            int[] indexs2 = new int[size];
            int index2 = 0;
            for(int i = 0;i < size;i++) {
                if(strs[i].equals(str1)){
                    indexs1[index1++] = i+1;
                }
                if(strs[i].equals(str2)){
                    indexs2[index2++] = i+1;
                }
            }
            if(index1 == 0 || index2 == 0){
                System.out.print(-1);
                return;
            }else{
                int min = Math.abs(indexs1[0] - indexs2[0]);
                for(int i = 0;i<indexs1.length;i++) {
                    for(int j = 0;j<indexs2.length;j++){
                        int abs = Math.abs(indexs1[i] - indexs2[j]);
                        if(min > abs){
                            min = abs;
                        }
                    }
                }
                System.out.print(min);
            }
        }
    }
}
